﻿using System;
using System.Web.UI;
using System.Collections;

namespace SimpleService.ORM
{
    public class Rows
    {
        public bool delete
        {
            get
            {
                return Convert.ToBoolean(_del);
            }
            set
            {
                if (!_del)
                {
                    if (value)
                    {
                        if (_val.Count > 0)
                        {
                            for (int i = 0; i < _val.Count; i++)
                            {
                                strings tmp = (strings)_val[i];
                                _del = _table.delete(tmp[0]);
                            }
                        }
                    }
                }
            }
        }

        ArrayList _key = new ArrayList(); 
        ArrayList _val = new ArrayList();	
        public bool _del = false;	

        PersistenceObject _table;
        
        public Rows(PersistenceObject _table) { this._table = _table; }
        public Rows(string[] fieldnames, strings[] data)
        {
            if (CheckLength())
            {

                int length = fieldnames.Length;

                for (int i = 0; i < length; i++)
                {
                    _key.Add(fieldnames[i]);
                    _val.Add(data[i]);
                }
            }
        }

        public void Set(int index1, string index2, string data)
        {

            if (index1 < this._key.Count)
            {
                strings ss = new strings(_table);
                ss = (strings)this._val[index1];
                ss.Set(index2, data);
            }
            else
            {
                _key.Add(this._key.Count);

                strings ss = new strings(_table);
                _val.Add((object)ss);
                ss = (strings)this._val[this._key.Count - 1];
                ss.Set(index2, data);

            }
        }


        public strings this[int i]
        {
            get
            {
                try
                {
                    return (strings)this._val[i];
                }
                catch (Exception e)
                {
                    strings ss = new strings(_table);
                    return (strings)ss;
                }
            }
        }

        public string this[string s]
        {
            get
            {
                strings ans = new strings(_table);
                ans = (strings)this._val[0];
                return ans[s];
            }

            set
            {
                if (!_del)
                {
                    strings ans = new strings(_table);
                    ans = (strings)this._val[0];

                    ans.update(s.Trim(), value);
                    ans.Set(s.Trim(), value);
                }
            }

        }


        private bool CheckLength()
        {

            bool ans = false;

            int keylenght = _key.Count;
            int vallenght = _val.Count;

            if (keylenght == vallenght) { ans = ans || true; } else { ans = false; }

            return ans;
        }

        public IEnumerator GetEnumerator()
        {
            return _val.GetEnumerator();
        }	
    }
}
